package cn.bytecollege.user.controller;

import cn.bytecollege.admin.service.ClassifyService;
import cn.bytecollege.admin.service.ProductService;
import cn.bytecollege.common.entity.*;
import cn.bytecollege.user.service.OrderDetailService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet(name = "ProductFindByIdController",value = "/user/findProduct")
public class ProductFindByIdController extends HttpServlet {
    private ProductService productService = null;
    private ClassifyService classifyService = null;
    public OrderDetailService orderDetailService=null;

    public ProductFindByIdController(){
        productService = new ProductService();
        classifyService = new ClassifyService();
        orderDetailService=new OrderDetailService();
    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id = request.getParameter("id");
        Integer productId = Integer.parseInt(id);
        Product product = productService.findClassifyById(productId);
        OrderDetail orderDetail=new OrderDetail();
        orderDetail.setProductId(productId);
        orderDetailService.save(orderDetail);
        orderDetail=orderDetailService.findByProductId(productId);
        request.setAttribute("product",product);
        List<Product> lists=productService.findAll();
        request.setAttribute("lists",lists);
        List<Product> linelists=null;
        if(lists.size()>3){
            linelists=lists.subList(0,3);
        }else {
            linelists=lists.subList(0,lists.size());
        }
        request.setAttribute("linelists",linelists);
        orderDetail.setProductNumber(1);
        request.setAttribute("orderDetail",orderDetail);
        request.getRequestDispatcher("/productview.jsp").forward(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
